//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LfmHullWhiteParameterization.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Legacy/Libormarketmodels/LiborForwardModelProcess.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Legacy/Libormarketmodels/LfmCovarianceParameterization.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModelProcess^ process, Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ capletVol, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IMatrix^>^ correlation, Microsoft::FSharp::Core::FSharpOption<UInt64>^ factors) : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
    CLiborForwardModelProcess^ _Cprocess;
    COptionletVolatilityStructure^ _CcapletVol;
    CMatrix^ _Ccorrelation;
    try
    {
#ifdef HANDLE
        _phLfmHullWhiteParameterization = NULL;
#endif
        _Cprocess = safe_cast<CLiborForwardModelProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::LiborForwardModelProcess>& _process = static_cast<boost::shared_ptr<QuantLib::LiborForwardModelProcess>&> (_Cprocess->GetShared ()); 
        _CcapletVol = safe_cast<COptionletVolatilityStructure^> (capletVol);
        _CcapletVol->Lock();
        boost::shared_ptr<QuantLib::OptionletVolatilityStructure>& _capletVol = static_cast<boost::shared_ptr<QuantLib::OptionletVolatilityStructure>&> (_CcapletVol->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IMatrix^>::IsSome::get (correlation))
        {
            _Ccorrelation = safe_cast<CMatrix^> (correlation->Value);
            _Ccorrelation->Lock();
        }
        QuantLib::Matrix& _correlation = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IMatrix^>::IsSome::get (correlation) ? static_cast<QuantLib::Matrix&> (_Ccorrelation->GetReference ()) : QuantLib::Matrix()); //1
        QuantLib::Size _factors = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (factors) ? (QuantLib::Size)ValueHelper::Convert (factors->Value) : 1); //4
        _ppLfmHullWhiteParameterization = new boost::shared_ptr<QuantLib::LfmHullWhiteParameterization> (new QuantLib::LfmHullWhiteParameterization ( _process,  _capletVol,  _correlation,  _factors ));
        SetLfmCovarianceParameterization (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_CcapletVol != nullptr) _CcapletVol->Unlock();
        if (_Ccorrelation != nullptr) _Ccorrelation->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (boost::shared_ptr<QuantLib::LfmHullWhiteParameterization>& childNative, Object^ owner) : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
#ifdef HANDLE
	_phLfmHullWhiteParameterization = NULL;
#endif
	_ppLfmHullWhiteParameterization = &childNative;
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
}
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (QuantLib::LfmHullWhiteParameterization& childNative, Object^ owner) : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
#ifdef HANDLE
	_phLfmHullWhiteParameterization = NULL;
#endif
	_ppLfmHullWhiteParameterization = new boost::shared_ptr<QuantLib::LfmHullWhiteParameterization> (&childNative);
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
    _LfmHullWhiteParameterizationOwner = owner;
    _LfmCovarianceParameterizationOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (CLfmHullWhiteParameterization^ copy) : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
#ifdef HANDLE
	_phLfmHullWhiteParameterization = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLfmHullWhiteParameterization = new boost::shared_ptr<QuantLib::LfmHullWhiteParameterization> (copy->GetShared());
        _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (System::Type^ t) : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
#ifdef HANDLE
	_phLfmHullWhiteParameterization = NULL;
#endif
	if (!t->IsSubclassOf(CLfmHullWhiteParameterization::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (QuantLib::Handle<QuantLib::LfmHullWhiteParameterization>& childNative, Object^ owner)  : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
	_phLfmHullWhiteParameterization = &childNative;
	_ppLfmHullWhiteParameterization = &static_cast<boost::shared_ptr<QuantLib::LfmHullWhiteParameterization>>(childNative.currentLink());
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
    _LfmHullWhiteParameterizationOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (QuantLib::Handle<QuantLib::LfmHullWhiteParameterization> childNative)  : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
	_phLfmHullWhiteParameterization = &childNative;
	_ppLfmHullWhiteParameterization = &static_cast<boost::shared_ptr<QuantLib::LfmHullWhiteParameterization>>(childNative.currentLink());
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::CLfmHullWhiteParameterization (QuantLib::LfmHullWhiteParameterization childNative)  : CLfmCovarianceParameterization(CLfmHullWhiteParameterization::typeid)
{
#ifdef HANDLE
	_phLfmHullWhiteParameterization = NULL;
#endif
	_ppLfmHullWhiteParameterization = new boost::shared_ptr<QuantLib::LfmHullWhiteParameterization> (new QuantLib::LfmHullWhiteParameterization (childNative));
    _ppLfmCovarianceParameterization = new boost::shared_ptr<QuantLib::LfmCovarianceParameterization> (boost::dynamic_pointer_cast<QuantLib::LfmCovarianceParameterization> (*_ppLfmHullWhiteParameterization));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::~CLfmHullWhiteParameterization ()
{
    if (_ppLfmHullWhiteParameterization != NULL)
    {
	    delete _ppLfmHullWhiteParameterization;
        _ppLfmHullWhiteParameterization = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::!CLfmHullWhiteParameterization ()
{
    if (_ppLfmHullWhiteParameterization != NULL)
    {
	    delete _ppLfmHullWhiteParameterization;
    }
}
QuantLib::LfmHullWhiteParameterization& Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::GetReference ()
{
    if (_ppLfmHullWhiteParameterization == NULL) throw gcnew NativeNullException ();
	return **_ppLfmHullWhiteParameterization;
}
boost::shared_ptr<QuantLib::LfmHullWhiteParameterization>& Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::GetShared ()
{
    if (_ppLfmHullWhiteParameterization == NULL) throw gcnew NativeNullException ();
	return *_ppLfmHullWhiteParameterization;
}
QuantLib::LfmHullWhiteParameterization* Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::GetPointer ()
{
    if (_ppLfmHullWhiteParameterization == NULL) throw gcnew NativeNullException ();
	return &**_ppLfmHullWhiteParameterization;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LfmHullWhiteParameterization>& Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::GetHandle ()
{
	if (_phLfmHullWhiteParameterization == NULL)
	{
		_phLfmHullWhiteParameterization = new Handle<QuantLib::LfmHullWhiteParameterization> (*_ppLfmHullWhiteParameterization);
	}
	return *_phLfmHullWhiteParameterization;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization::HasNative () 
{
	return (_ppLfmHullWhiteParameterization != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILfmHullWhiteParameterization^ Cephei::QL::Legacy::Libormarketmodels::CLfmHullWhiteParameterization_Factory::Create (Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModelProcess^ process, Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ capletVol, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IMatrix^>^ correlation, Microsoft::FSharp::Core::FSharpOption<UInt64>^ factors)
{
    return gcnew CLfmHullWhiteParameterization ( process,  capletVol,  correlation,  factors);
}
